﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Maxleaf.EDI.Domain;
using Maxleaf.EDI.Domain.EDI;

namespace Maxleaf.EDI.Core.Handlers.QueryHandlers
{
    public class GetAllUsersQueryHandler : IQueryHandler<Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllUsersParameters, Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllUsersResult>
    {
        private readonly IUnitOfWorkProvider unitOfWorkProvider;
        private readonly IRepository<DBDesc> dbRepository;
        private readonly IRepository<User_DB> userRepository;

        public GetAllUsersQueryHandler(
            IRepository<DBDesc> dbRepository,
            IRepository<User_DB> userRepository,
            IUnitOfWorkProvider unitOfWorkProvider)
        {
            this.dbRepository = dbRepository;
            this.userRepository = userRepository;
            this.unitOfWorkProvider = unitOfWorkProvider;
        }

        public Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllUsersResult Handle(Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllUsersParameters parameters)
        {
            using (var unitOfWork = this.unitOfWorkProvider.GetEDIUnitOfWork())
            {
                var users = this.userRepository.GetAll(unitOfWork);

                return new Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllUsersResult() { Users = users.Select(o => new Maxleaf.EDI.API.Contract.Admin.Parameters.GetAllUsersResult.User { UserName = o.UserName ,DBIds = o.DBID }).ToList() };
            }
        }
    }
}
